package cqrtplm.controller;

import com.github.pagehelper.PageInfo;
import cqrtplm.entity.CategoryPictureDO;
import cqrtplm.service.CategoryPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ty.basic.response.ResponseResult;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/rest/api/category")

public class CategoryPictureController {

    @Autowired
    private CategoryPictureService categoryPictureService;

    /**
     * 新增品类图片信息
     */
    @PostMapping("/insert")
    public ResponseResult save(@RequestBody CategoryPictureDO categoryPictureDO) {
        boolean success = categoryPictureService.save(categoryPictureDO);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.fail("400","新增失败");
    }

    /**
     * 根据OID查询品类图片信息
     */
    @GetMapping("/select")
    public ResponseResult getByOid(@RequestParam String oid) {
        CategoryPictureDO categoryPicture = categoryPictureService.getByOid(oid);
        if (categoryPicture != null) {
            return ResponseResult.success();
        }
        return ResponseResult.fail("400","数据不存在");
    }

    /**
     * 根据品类码查询品类图片信息
     */
    @GetMapping("/category")
    public ResponseResult getByCategory(@RequestParam String category) {
        CategoryPictureDO categoryPicture = categoryPictureService.getByCategory(category);
        if (categoryPicture != null) {
            return ResponseResult.success();
        }
        return ResponseResult.fail("400","数据不存在");
    }

    /**
     * 查询所有品类图片信息
     */
    @GetMapping("/list")
    public ResponseResult listAll(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                                  @RequestParam(defaultValue = "10",value = "pageSize") Integer pageSize,
                                  @RequestParam(required = false,value = "keyWork")String keyWork) {
        PageInfo<CategoryPictureDO> pageInfo = categoryPictureService.listAll(pageNum, pageSize, keyWork);
        return ResponseResult.success(pageInfo);
    }

    /**
     * 更新品类图片信息
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody CategoryPictureDO categoryPictureDO) {
        boolean success = categoryPictureService.update(categoryPictureDO);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.fail("400","更新失败");
    }
}